package com.gis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gis.entity.StationData;
import com.gis.result.ResponseResult;
import com.gis.service.StationDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;



@RestController
@RequiredArgsConstructor
@RequestMapping("/station_data")
@CrossOrigin(origins = "*")
@Tag(name = "站点", description = "站点数据接口")
public class StationDataController {
    @Autowired
    private StationDataService stationDataService;
    
    @Operation(summary = "添加站点数据")
    @Parameters({
        @Parameter(name = "stationData", description = "站点数据", required = true)
    })
    @PostMapping("/add")
    public ResponseResult<String> AddStationData(@RequestBody StationData stationData) {
        stationDataService.save(stationData);
        return ResponseResult.success("insert successfully");
    }

    @Operation(summary = "根据ID获取站点数据")
    @Parameters({
        @Parameter(name = "id", description = "站点ID", required = true)
    })
    @GetMapping("/getByID")
    public ResponseResult<StationData> GetAllStation(@RequestParam Integer id) {
        StationData stationData = stationDataService.getById(id);
        return ResponseResult.success("sucess", stationData);
    }

    @Operation(summary = "根据站点名称获取站点数据")
    @Parameters({
        @Parameter(name = "name", description = "站点名称", required = true)
    })
    @GetMapping("/getByName")
    public ResponseResult<StationData> GetByProperty(@RequestParam String name) {
        QueryWrapper<StationData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).last("LIMIT 1");
        StationData stationData = stationDataService.getOne(queryWrapper);
        return ResponseResult.success("sucess", stationData);
    }

    @Operation(summary = "获取所有站点数据")
    @GetMapping("/getAll")
    public ResponseResult<List<StationData>> GetAll() {
        // QueryWrapper<StationData> queryWrapper = new QueryWrapper<>();
        List<StationData> stationDataList = stationDataService.list();
        return ResponseResult.success("sucess", stationDataList);
    }
}
